package main

import (
	"fmt"

	"github.com/shirou/gopsutil/cpu"
	"github.com/shirou/gopsutil/disk"
	"github.com/shirou/gopsutil/host"
	"github.com/shirou/gopsutil/mem"
	"encoding/json"
)

type SysInfo struct {
	ip string
	Os string //系统版本
}

type DiskInfo struct {
	Diskfree  string `json:"free"`
	Disktotal string `json:"total"`
	Diskper   string `json:"per"`
}

type MemInfo struct {
	MemTotal   string `json:"total"`
	MemAvail   string `json:"avail"`
	MemUsedPer string `json:"per"`
}
type CupInfo struct {
	CpuUs string `json:"us"`
	CpuSy string `json:"sy"`
	CPuId string `json:"id"`
	CpuIo string `json:"io"`
}

/**
	统一的返回数据格式[{}]
 */
func GetSysMem() interface{} {
	/**
		获取系统信息
	 */
	arr := make([]interface{}, 1)
	v, _ := mem.VirtualMemory()
	data := MemInfo{fmt.Sprintf("%0.0f", float64(v.Total)/1024/1024/1024) + "G",
		fmt.Sprintf("%0.1f", float64(v.Available)/1024/1024/1024) + "G",
		fmt.Sprintf("%0.0f", v.UsedPercent) + "%"}

	arr[0] = data
	return arr
}

func GetSysCpu() interface{} {
	timesStats, _ := cpu.Times(true)
	data := CupInfo{fmt.Sprintf("%0.0f", timesStats[0].User),
		fmt.Sprintf("%0.0f", timesStats[0].System),
		fmt.Sprintf("%0.0f", timesStats[0].Idle),
		fmt.Sprintf("%0.0f", timesStats[0].Iowait)}

	arr := make([]interface{}, 1)
	arr[0] = data
	return arr
}

func GetSysDisk() interface{} {
	stats, _ := disk.Partitions(true)
	arr := make([]interface{}, len(stats))
	for k, v := range stats {
		//data := make(map[string]string)
		usage, _ := disk.Usage(v.Device)
		info := DiskInfo{Diskfree: fmt.Sprintf("%0.1d", usage.Free/1024/1024/1024),
			Disktotal: fmt.Sprintf("%0.1d", usage.Total/1024/1024/1024),
			Diskper: fmt.Sprintf("%0.1f", usage.UsedPercent)}

		arr[k] = info
	}

	return arr
}
func GetSysHost()  {
	stat, _ := host.Info()
	fmt.Println(stat)
}

type Info struct {
	Cpu  interface{} `json:"cpu"`
	Disk interface{} `json:"disk"`
	Mem  interface{} `json:"mem"`
}

func main() {
	info := Info{Cpu: GetSysCpu(), Disk: GetSysDisk(), Mem: GetSysMem()}
	marshal, _ := json.Marshal(info)
	fmt.Println(string(marshal))
	//GetSysHost()

}
